<?php

namespace Home\Model;

use Think\Model;

class GoodsModel extends Model {

    function get_info($id) {
        $goods = $this->field( "ecm_store.state,ecm_goods.*,ecm_coupon_codes.store_id as sto_id,ifnull(ecm_coupon_codes.ico,'') as ico")
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store  ON ' . C('DB_PREFIX') . 'goods.store_id = ' . C('DB_PREFIX') . 'store.store_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'discount  ON ' . C('DB_PREFIX') . 'goods.goods_id = ' . C('DB_PREFIX') . 'discount.goods_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'coupon_codes  ON ' . C('DB_PREFIX') . 'discount.coupon_codes_id = ' . C('DB_PREFIX') . 'coupon_codes.id and ' . C('DB_PREFIX') . 'coupon_codes.enabled = 1')
            ->where('ecm_goods.goods_id=' . $id)
            ->group(C('DB_PREFIX').'goods.goods_id')
            ->find();
        if ($goods) {
            /* 商品规格 */
            $Spec_mod = M('goods_spec');
            $cond['ecm_goods_spec.goods_id'] = $id;
            $cond['ecm_goods_spec.stock'] = array('neq',0);
            $cond['ecm_goods_spec.price'] = array('neq',0);

            $specs = $Spec_mod->field('ecm_storestock.*,ecm_goods_spec.*,ecm_size_order.size_order,ecm_coupon_codes.store_id as sto_id')
                ->join('ecm_storestock on ecm_goods_spec.spec_3value = ecm_storestock.storestock_id')
                ->join('left join ecm_size_order on ecm_goods_spec.spec_2 = ecm_size_order.size_name')
                ->join('left join ecm_discount on ecm_goods_spec.spec_id = ecm_discount.spec_id')
                ->join('left join ecm_coupon_codes on ecm_discount.coupon_codes_id = ecm_coupon_codes.id and ecm_coupon_codes.enabled = 1')
                ->order('ecm_size_order.size_order')
                ->group('ecm_goods_spec.spec_id')
                ->where($cond)
                ->select();
            if(!$specs[0]['size_order'] && $specs){
                $last_names = array_column($specs,'spec_2');
                array_multisort($last_names,SORT_ASC,$specs);
            }

            $other_spec = array();
            foreach ($specs as $k => $spec) {
                if($spec['stock_style'] == 1 || $spec['stock_style'] == 2){
                    if(floatval($spec['price']) > 5000){
                        unset($specs[$k]);
                        continue;
                    }
                }
                //临时
                $res_wish_close = M('mall_config')->field('code,value')->where(array('code'=>'wish_close'))->find();
                if($res_wish_close['value'] != 1){
                    $specs[$k]['stock_style'] = 4;
                }
                if($goods['wish_close'] == 0){
                    if ($spec['stock_style'] == 3){
                        $specs[$k]['stock_style'] = 4;
                    }
                }
                if ($spec['spec_id'] != $goods['default_spec']) {
                    if ($goods['market_price'] > 0) {
                        $spec['market_price'] = $goods['market_price'] * $spec['price'];
                    }
                    $other_spec[] = $spec;
                } else {
                    $default_spec = $spec;
                }
            }

            if ($goods['market_price'] > 0) {
                $default_spec['market_price'] = $goods['market_price'] * $default_spec['price'];
            }
            $goods['_specs'] = array_merge(array($default_spec), $other_spec);

            /* 商品图片 */
            $Image_mod = M('goods_image');
            $_images = $Image_mod->where("goods_id = '$id'")->order('sort_order desc')->select();
            $goods['_images'] = array_values($_images);

            /* 统计情况 */
            $stat_info = M('goods_statistics')->where('goods_id='.$id)->find();
            $goods = array_merge($goods, $stat_info);
        }
        return $goods;
    }
    function get_info_test($id) {
        $goods = $this->field( "ecm_store.state,ecm_goods.*,ecm_coupon_codes.store_id as sto_id,ifnull(ecm_coupon_codes.ico,'') as ico")
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store  ON ' . C('DB_PREFIX') . 'goods.store_id = ' . C('DB_PREFIX') . 'store.store_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'discount  ON ' . C('DB_PREFIX') . 'goods.goods_id = ' . C('DB_PREFIX') . 'discount.goods_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'coupon_codes  ON ' . C('DB_PREFIX') . 'discount.coupon_codes_id = ' . C('DB_PREFIX') . 'coupon_codes.id')
            ->where('ecm_goods.goods_id=' . $id)
            ->group(C('DB_PREFIX').'goods.goods_id')
            ->find();
        if ($goods) {
            /* 商品规格 */
            $Spec_mod = M('goods_spec');
            $cond['ecm_goods_spec.goods_id'] = $id;
            $cond['ecm_goods_spec.stock'] = array('neq',0);
            $cond['ecm_goods_spec.price'] = array('neq',0);

            $specs = $Spec_mod->field('ecm_storestock.*,ecm_goods_spec.*,ecm_size_order.size_order,ecm_coupon_codes.store_id as sto_id')
                ->join('ecm_storestock on ecm_goods_spec.spec_3value = ecm_storestock.storestock_id')
                ->join('left join ecm_size_order on ecm_goods_spec.spec_2 = ecm_size_order.size_name')
                ->join('left join ecm_discount on ecm_goods_spec.spec_id = ecm_discount.spec_id')
                ->join('left join ecm_coupon_codes on ecm_discount.coupon_codes_id = ecm_coupon_codes.id')
                ->order('ecm_size_order.size_order')
                ->group('ecm_goods_spec.spec_id')
                ->where($cond)
                ->select();
            if(!$specs[0]['size_order'] && $specs){
                $last_names = array_column($specs,'spec_2');
                array_multisort($last_names,SORT_ASC,$specs);
            }

            $other_spec = array();
            foreach ($specs as $k => $spec) {
                if($spec['stock_style'] == 1 || $spec['stock_style'] == 2){
                    if(floatval($spec['price']) > 5000){
                        unset($specs[$k]);
                        continue;
                    }
                }
                if ($spec['spec_id'] != $goods['default_spec']) {
                    if ($goods['market_price'] > 0) {
                        $spec['market_price'] = $goods['market_price'] * $spec['price'];
                    }
                    $other_spec[] = $spec;
                } else {
                    $default_spec = $spec;
                }
            }

            if ($goods['market_price'] > 0) {
                $default_spec['market_price'] = $goods['market_price'] * $default_spec['price'];
            }
            $goods['_specs'] = array_merge(array($default_spec), $other_spec);

            /* 商品图片 */
            $Image_mod = M('goods_image');
            $_images = $Image_mod->where("goods_id = '$id'")->order('sort_order desc')->select();
            $goods['_images'] = array_values($_images);

            /* 统计情况 */
            $stat_info = M('goods_statistics')->where('goods_id='.$id)->find();
            $goods = array_merge($goods, $stat_info);
        }
        return $goods;
    }
    function get_isSaled($id){
        $Spec_mod = M('goods_spec');
        $cond['ecm_goods_spec.goods_id'] = $id;
        $cond['ecm_goods_spec.stock'] = array('neq',0);
        $cond['ecm_goods_spec.price'] = array('neq',0);

        $specs = $Spec_mod->field('ecm_storestock.*,ecm_goods_spec.*')
            ->join('ecm_storestock on ecm_goods_spec.spec_3value = ecm_storestock.storestock_id')
            ->where($cond)
            ->select();
        foreach ($specs as $k => $spec) {
            if ($spec['stock_style'] == 1 || $spec['stock_style'] == 2) {
                if (floatval($spec['price']) > 5000) {
                    unset($specs[$k]);
                    continue;
                }
            }
        }

        return $specs ? true : false;
    }


}

?>
